﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net;
using System.Net.Http;

namespace ConsoleApp1
{
    internal class Program
    {
        
        static void Main(string[] args)
        {
            CookieContainer c = new CookieContainer();
            HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create("HTTP://WWW.163.COM");
            webRequest2.Timeout = 20000;
            CookieCollection myCookieCollection = new CookieCollection();
            Cookie c1 = new Cookie("DAFPASS", "XOow8%2F7jrr4%3D", "/"); // Password   :   Kaiser   
            Cookie c2 = new Cookie("DAFLOGIN", "peter%40anne%2Ecom", "/"); // UserName   :   peter@anne.com  
            myCookieCollection.Add(c1);
            myCookieCollection.Add(c2);
            webRequest2.CookieContainer = c;
            webRequest2.CookieContainer.Add(new Uri("http://www.abberose.com"), myCookieCollection);


            CookieCollection cc = c.GetCookies(new Uri("http://www.abberose.com"));


            Func<IPAddress, bool> func = e => e.AddressFamily.ToString() == "InterNetwork";
            IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(func);
           
            if (ip == null)
            {
                Console.WriteLine("===="+ip.ToString());
            }

            var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (var networkInterface in networkInterfaces)
            {
                Console.WriteLine(networkInterface.GetPhysicalAddress().ToString());
                Console.WriteLine(networkInterface.GetIPProperties().GetIPv4Properties().ToString());
            }

            string hostName = Dns.GetHostName();
            IPHostEntry hostEntry = Dns.GetHostEntry(hostName);

            foreach (IPAddress ipAddress in hostEntry.AddressList)
            {
                if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
                {
                    Console.WriteLine("本地IP地址: " + ipAddress.ToString());
                }
            }

            /*  if (networkInterfaces.Length > 0)
              {
                  var macAddress = networkInterfaces[0].GetPhysicalAddress().ToString();
                  Console.WriteLine($"MacAddress: {macAddress}");
              }
              else
              {
                  Console.WriteLine("No network interface found.");
              }*/




            /*string chromeMessage = OpenStandardStreamIn();

            Stream output = Console.OpenStandardOutput();
            String mac = "{\"mac\":\"" + GetMacAddress() + "\"}";
            byte[] bytes = Encoding.UTF8.GetBytes(mac);
            output.WriteByte((byte)((bytes.Length >> 0) & 0xFF));
            output.WriteByte((byte)((bytes.Length >> 8) & 0xFF));
            output.WriteByte((byte)((bytes.Length >> 16) & 0xFF));
            output.WriteByte((byte)((bytes.Length >> 24) & 0xFF));
            output.Write(bytes, 0, bytes.Length);
            output.Flush();
            output.Close();*/
            return;
        }

        private static string OpenStandardStreamIn()
        {
            //// We need to read first 4 bytes for length information
            Stream stdin = Console.OpenStandardInput();
            int length = 0;
            byte[] bytes = new byte[4];
            stdin.Read(bytes, 0, 4);
            length = System.BitConverter.ToInt32(bytes, 0);

            string input = "";
            for (int i = 0; i < length; i++)
            {
                input += (char)stdin.ReadByte();
            }

            return input;
        }
        public static string GetMacAddress()
        {
            try
            {
                string strMac = string.Empty;
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        strMac = mo["MacAddress"].ToString();
                    }
                }
                moc = null;
                mc = null;
                return strMac;
            }
            catch
            {
                return "unknown";
            }
        }
    }
}
